//GameStateMap class
#include "GameStateMap.h"
#include "GameStateManager.h"

//GameStateMap Instance
GameStateMap GameStateMap::Instance;

//default constructor
GameStateMap::GameStateMap() {

    Player1.MoveLeft = false;
    Player1.MoveRight = false;
    Player1.MoveDown = false;
    Player1.MoveUp = false;

    //Set rock positions
    Rock.X=100;    Rock.Y=125;
    Rock2.X=100;   Rock2.Y=150;
    Rock3.X=100;   Rock3.Y=175;
    Rock4.X=100;   Rock4.Y=200;
    Rock5.X=250;   Rock5.Y=430;
    Rock6.X=300;   Rock6.Y=430;
    Rock7.X=200;   Rock7.Y=450;
    Rock8.X=250;   Rock8.Y=480;

    //Set initial player1 position
    Player1.X=300; Player1.Y=300;

    //Set Player2 position
    Player2.X=500;  Player2.Y=250;

    //Set trees positions
    for(int i =0; i <38; i++){
        Tree[i].X = 0;
        Tree[i].Y = 50 * i;
        }


//Set the Treasure position
    Treasure1.X=250;  Treasure1.Y=450;





}

//OnKeyDown
void GameStateMap::OnKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode) {

    srand(time(NULL));
    switchState = false;

    switch(sym) {
        case SDLK_LEFT: {
            Player1.MoveLeft = true;
            random = (rand() % 25 + 1);
            if (random < 20 && random >15)
                switchState = true;
            break;

        }

        case SDLK_RIGHT: {
            Player1.MoveRight = true;
            random = rand() % 25 + 1;
            if (random > 3 && random < 8)
                switchState = true;

            break;
        }

        case SDLK_DOWN: {
            Player1.MoveDown = true;
            random = rand() % 25 + 1;
            if (random > 8 && random < 13)
                switchState = true;
            break;
            }

        case SDLK_UP: {
            Player1.MoveUp = true;
            random = rand() % 25 + 1;
            if (random < 23 && random > 18)
                switchState = true;
            break;
        }

        case SDLK_SPACE: {
            break;
        }

        case SDLK_q: {
            break;
        }

        default: {
        }
    }
}

//OnKeyUp
void GameStateMap::OnKeyUp(SDLKey sym, SDLMod mod, Uint16 unicode) {
    switch(sym) {
        case SDLK_LEFT: {
            Player1.MoveLeft = false;
            Player1.MoveRight = false;
            Player1.MoveDown = false;
            Player1.MoveUp = false;
            if (switchState == true)
                GameStateManager::SetActiveGameState(GAMESTATE_BATTLE);
            break;
        }

        case SDLK_RIGHT: {
            Player1.MoveLeft = false;
            Player1.MoveRight = false;
            Player1.MoveDown = false;
            Player1.MoveUp = false;
            if (switchState == true)
                GameStateManager::SetActiveGameState(GAMESTATE_BATTLE);
            break;
        }

        case SDLK_DOWN: {
            Player1.MoveLeft = false;
            Player1.MoveRight = false;
            Player1.MoveDown = false;
            Player1.MoveUp = false;
            if (switchState == true)
                GameStateManager::SetActiveGameState(GAMESTATE_BATTLE);
        break;
        }

        case SDLK_UP: {
            Player1.MoveLeft = false;
            Player1.MoveRight = false;
            Player1.MoveDown = false;
            Player1.MoveUp = false;
            if (switchState == true)
                GameStateManager::SetActiveGameState(GAMESTATE_BATTLE);
        break;
        }

        case SDLK_SPACE: {
            GameStateManager::SetActiveGameState(GAMESTATE_BATTLE);
            break;
        }

        case SDLK_q: {
            GameStateManager::SetActiveGameState(0);
            break;
        }

        default: {
        }

        }
}

//OnActivate
void GameStateMap::OnActivate() {
    if(Area::AreaControl.OnLoad("./maps/1.area") == false) {
        return;
    }

    if(Player1.OnLoad("sprites.png", 32, 32, 3) == false) {
        return;
    }

    if(Player2.OnLoad("evilsprites.png", 32, 32, 3) == false) {
        return;
    }

//Load rocks-
    if(Rock.OnLoad("rock.png", 32, 24, 3) == false) {
        return;
    }
    if(Rock2.OnLoad("rock.png", 32, 24, 3) == false) {
        return;
    }
    if(Rock3.OnLoad("rock.png", 32, 24, 3) == false) {
        return;
    }
    if(Rock4.OnLoad("rock.png", 32, 24, 3) == false) {
        return;
    }
    if(Rock5.OnLoad("rock.png", 32, 24, 3) == false) {
        return;
    }
    if(Rock6.OnLoad("rock.png", 32, 24, 3) == false) {
        return;
    }
    if(Rock7.OnLoad("rock.png", 32, 24, 3) == false) {
        return;
    }
    if(Rock8.OnLoad("rock.png", 32, 24, 3) == false) {
        return;
    }

//Load trees
for (int i = 0; i < 38; i++){
    if(Tree[i].OnLoad("Tree1.png", 63, 75, 3) == false)
        return;
    }

//Load Treasure
    if(Treasure1.OnLoad("treasure.png", 32, 28, 3) == false) {
        return;
    }


    Entity::EntityList.push_back(&Player1);
    Entity::EntityList.push_back(&Player2);
    Entity::EntityList.push_back(&Rock);
    Entity::EntityList.push_back(&Rock2);
    Entity::EntityList.push_back(&Rock3);
    Entity::EntityList.push_back(&Rock4);
    Entity::EntityList.push_back(&Rock5);
    Entity::EntityList.push_back(&Rock6);
    Entity::EntityList.push_back(&Rock7);
    Entity::EntityList.push_back(&Rock8);

    for (int i = 0; i < 38; i++){
        Entity::EntityList.push_back(&Tree[i]);
        }

    Entity::EntityList.push_back(&Treasure1);


    Camera::CameraControl.TargetMode = TARGET_MODE_CENTER;
    Camera::CameraControl.SetTarget(&Player1.X, &Player1.Y);
}

//OnDeactivate
void GameStateMap::OnDeactivate() {
    Area::AreaControl.OnCleanup();

    for(int i = 0;i < Entity::EntityList.size();i++) {
        if(!Entity::EntityList[i]) continue;

        Entity::EntityList[i]->OnCleanup();
    }

    Entity::EntityList.clear();
    //this creates a black box to cover old graphics (due to bug?)
    SDL_FillRect(SDL_GetVideoSurface(), NULL, 0);
}

//OnLoop
void GameStateMap::OnLoop() {
    for(int i = 0;i < Entity::EntityList.size();i++) {
        if(!Entity::EntityList[i]) continue;

        Entity::EntityList[i]->OnLoop();
    }

    //Constrain Player from going off left edge of map
    if (Player1.X<0)
        Player1.X=0;
    //Constrain Player from going off top edge of map
    if (Player1.Y<0)
        Player1.Y=0;
    //Contstrain Player from going off bottom edge of map
    if (Player1.Y>1875)
        Player1.Y=1875;
    //Constrain Player from going off right edge of map
    if (Player1.X>1875)
        Player1.X=1875;

    //Check for collisions
    for(int i = 0;i < EntityCol::EntityColList.size();i++) {
        Entity* Entity1 = EntityCol::EntityColList[i].Entity1;
        Entity* Entity2 = EntityCol::EntityColList[i].Entity2;

        if(Entity1 == NULL || Entity2 == NULL) continue;

        if(Entity1->OnCollision(Entity2)) {
            Entity1->OnCollision(Entity2);
        }
    }

    EntityCol::EntityColList.clear();
}

//OnRender
void GameStateMap::OnRender(SDL_Surface* Surf_Display) {
    SDL_Rect Rect;
    Rect.x = 0;
    Rect.y = 0;
    Rect.w = WWIDTH;
    Rect.h = WHEIGHT;

    SDL_FillRect(Surf_Display, &Rect, 0);

    Area::AreaControl.OnRender(Surf_Display, -Camera::CameraControl.GetX(), -Camera::CameraControl.GetY());

    for(int i = 0;i < Entity::EntityList.size();i++) {
        if(!Entity::EntityList[i]) continue;
            if(Surf_Display)
                  Entity::EntityList[i]->OnRender(Surf_Display);
    }
}

//GetInstance
GameStateMap* GameStateMap::GetInstance() {
    return &Instance;
}

